//
// Created by tcj on 2022/3/26.
//

#include "sylar/bytearray.h"
#include "sylar/sylar.h"

static sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();
void test(){
#define XX(type,len,write_fun,read_fun,base_len)    {               \
    std::vector<type> vec;                                       \
    for(int i=0;i<len;++i){                                          \
        vec.push_back(rand());                                      \
    }                                                               \
    sylar::ByteArray::ptr ba(new sylar::ByteArray(base_len));       \
    for(auto& i:vec){                                               \
        ba->write_fun(i);                                         \
    }                                                               \
    ba->setPosition(0);                                             \
    for(size_t i=0;i<vec.size();++i){                               \
        type v = ba->read_fun();                               \
        SYLAR_ASSERT(v==vec[i]);                                   \
    }                                                               \
    SYLAR_ASSERT(ba->getReadSize() ==0);                           \
    SYLAR_LOG_INFO(g_logger)<< #write_fun "/" #read_fun    \
                                    "(" #type") len="<<len                \
                                    <<" base_len="<<base_len       \
                                    <<" size="<<ba->getSize();\
}

//    XX(int8_t,100, WriteFint8,readFint8,1);
//    XX(uint8_t,100, WriteFuint8,readFuint8,1);
//    XX(int16_t ,100, WriteFint16,readFint16,1);
//    XX(uint16_t ,100, WriteFuint16,readFuint16,1);
//    XX(int32_t ,100, WriteFint32,readFuint32,1);
//    XX(uint32_t ,100, WriteFuint32,readFuint32,1);
//    XX(int64_t ,100, WriteFint64,readFuint64,1);
//    XX(uint64_t ,100, WriteFuint64,readFuint64,1);

//不能执行
//    XX(int32_t ,100, WriteInt32,readInt32,1);
//    XX(uint32_t ,100, WriteUint32,readUint32,1);
//    XX(int64_t ,100, WriteInt64,readInt64,1);
//    XX(uint64_t ,100, WriteUint64,readUint64,1);

//endif

#undef XX


#define XX(type,len,write_fun,read_fun,base_len)    {               \
    std::vector<type> vec;                                       \
    for(int i=0;i<len;++i){                                          \
        vec.push_back(rand());                                      \
    }                                                               \
    sylar::ByteArray::ptr ba(new sylar::ByteArray(base_len));       \
    for(auto& i:vec){                                               \
        ba->write_fun(i);                                         \
    }                                                               \
    ba->setPosition(0);                                             \
    for(size_t i=0;i<vec.size();++i){                               \
        type v = ba->read_fun();                               \
        SYLAR_ASSERT(v==vec[i]);                                   \
    }                                                               \
    SYLAR_ASSERT(ba->getReadSize() ==0);                           \
    SYLAR_LOG_INFO(g_logger)<< #write_fun "/" #read_fun    \
                                    "(" #type") len="<<len                \
                                    <<" base_len="<<base_len       \
                                    <<" size="<<ba->getSize();      \
    ba->setPosition(0);                                             \
    SYLAR_ASSERT(ba->WriteToFile("/tmp/"#type "_" #len "-" #read_fun ".dat"));    \
    sylar::ByteArray::ptr ba2(new sylar::ByteArray(base_len*2));    \
    ba2->setPosition(0);                                                                \
    SYLAR_ASSERT(ba->toString()==ba2->toString());  \
    SYLAR_ASSERT(ba->getPosition()==0);\
    SYLAR_ASSERT(ba2->getPosition()==0);\
}


    XX(int8_t,100, WriteFint8,readFint8,1);
    XX(uint8_t,100, WriteFuint8,readFuint8,1);
    XX(int16_t ,100, WriteFint16,readFint16,1);
    XX(uint16_t ,100, WriteFuint16,readFuint16,1);
    XX(int32_t ,100, WriteFint32,readFuint32,1);
    XX(uint32_t ,100, WriteFuint32,readFuint32,1);
    XX(int64_t ,100, WriteFint64,readFuint64,1);
    XX(uint64_t ,100, WriteFuint64,readFuint64,1);
#undef XX


}

int main(int argc,char** argv){
    test();
    return 0;
}